﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CeA.Reports.Atendimento.Web.ServerControls;
using CeA.Reports.Atendimento.Web.Helpers;
using CeA.Reports.Atendimento.Web.WebControls;
using Qtech.Reports.Atendimento.Services;
using CeA.Reports.Atendimento.DataAccess.Criteria;
using CeA.Reports.Atendimento.DataAccess.DTO;

namespace CeA.Reports.Atendimento.Web.admin {
    public partial class usuario : System.Web.UI.Page {
        protected void Page_Init(object sender, EventArgs e)                            {

            cmdPesquisar.Click += new EventHandler(cmdPesquisar_Click);
            cmdLimpar.Click += new EventHandler(cmdLimpar_Click);
            cmdEditar.Click += new EventHandler(cmdEditar_Click);
            cmdExcluir.Click += new EventHandler(cmdExcluir_Click);

            dataList.ItemCommand += new DataListCommandEventHandler(dataList_ItemCommand);
            MessageBox.Command += new CeA.Reports.Atendimento.Web.WebControls.MessageBox.CommandEventHandler(MessageBox_Command);

        }

        
        protected void Page_Load(object sender, EventArgs e)                            {
            menuInterno.addMenuItem("Incluir Novo Usuário", "/admin/usuariocadastro.aspx");
        }


        protected void cmdPesquisar_Click(object sender, EventArgs e)                   {
            CarregarLista();
        }


        protected void cmdLimpar_Click(object sender, EventArgs e)                      {
            txtLogin.Text = string.Empty;
            txtNome.Text = string.Empty;

            this.CodigoUsuario = 0;

            txtNome.Focus();

            dataList.DataSource = null;
            dataList.DataBind();

            gridHeader.Visible = false;
            gridScroll.Visible = false;
            editControls.Visible = false;
        }


        protected void cmdEditar_Click(object sender, EventArgs e)                      {
            List<GroupRadioButton> rbtList = DatalistHelper.FindControl<GroupRadioButton>(dataList, "rbtItem");

            foreach (GroupRadioButton rbt in rbtList) {
                if (rbt.Checked) {
                    this.CodigoUsuario = Convert.ToInt32(rbt.Text);
                    this.PageMode = "EDIT";
                    Server.Transfer("usuariocadastro.aspx", true);
                }
            }

            MessageBox.pop("É necessário selecionar um registro para editar!");
        }
        

        protected void cmdExcluir_Click(object sender, EventArgs e)                     {
            List<GroupRadioButton> rbtList = DatalistHelper.FindControl<GroupRadioButton>(dataList, "rbtItem");

            foreach (GroupRadioButton rbt in rbtList) {
                if (rbt.Checked) {
                    this.CodigoUsuario = Convert.ToInt32(rbt.Text);

                    MessageBox.pop("Deseja excluir o registro selecionado?", MessageBoxButtoms.YesNo);
                    MessageBox.CommandName = "DELETE";

                    return;
                }
            }

            MessageBox.pop("É necessário selecionar um registro para excluir!");
        }


        protected void dataList_ItemCommand(object source, DataListCommandEventArgs e)  {
            if (e.CommandName == "VIEW") {
                this.CodigoUsuario = Convert.ToInt32(e.CommandArgument);
                this.PageMode = "VIEW";
                Server.Transfer("usuariocadastro.aspx", true);
            }
        }


        protected void MessageBox_Command(object sender, MessageBoxEventArgs e)         {
            if (e.CommandName == "DELETE" && e.Command == MessageBoxCommand.Yes) {
                try {
                    CadastroFacade.RemoveUsuario(this.CodigoUsuario);
                    CarregarLista();
                }
                catch (InvalidOperationException ex) {
                    MessageBox.pop(ex.Message);
                }
            }
        }


        private void CarregarLista()                                                    {
            UsuarioCriteria filter = new UsuarioCriteria();
            filter.Login = txtLogin.Text;
            filter.Nome = txtNome.Text;
            filter.Email = txtEmail.Text;

            List<UsuarioDTO> ds = CadastroFacade.FindUsuario(filter);

            dataList.DataSource = ds;
            dataList.DataBind();

            // DataList footer hack!
            if (ds.Count > 0) {
                gridHeader.Visible = true;
                gridScroll.Visible = true;
                editControls.Visible = true;
            }
            else {
                gridHeader.Visible = false;
                gridScroll.Visible = false;
                editControls.Visible = false;
            }
        }


        #region -- Page Properties --------------------------------------------


        public int CodigoUsuario    {
            get {
                if (ViewState["m_codigoUsuario"] != null)
                    return Convert.ToInt32(ViewState["m_codigoUsuario"]);
                return 0;
            }
            set {
                ViewState.Add("m_codigoUsuario", value);
            }
        }


        public string PageMode      {
            get {
                if (ViewState["m_pageMode"] != null)
                    return Convert.ToString(ViewState["m_pageMode"]);
                return "VIEW";
            }
            set {
                ViewState.Add("m_pageMode", value);
            }
        }


        #endregion
    }
}
